<template>
  <div class="container">
    <div>
      <el-button
        @click="onShowSignDialog"
        icon="el-icon-plus"
      >签约</el-button>
      <el-button
        @click="onRecharge"
        icon="el-icon-plus"
      >充值</el-button>
    </div>
    <div class>
      <h1>签约信息</h1>
      <display-table :model="signInfo"></display-table>
    </div>
    <div class>
      <h1>记账本</h1>
      <display-table :model="accountBook"></display-table>
    </div>
    <div class="pb-3">
      <h1>备付金账户</h1>
      <display-table :model="extCardInfo"></display-table>
    </div>
    <el-dialog
      :visible.sync="signVisible"
      title="支付宝签约"
      width="38%"
    >
      <div class="row jcc">
        <iframe
          :src="signUrl"
          frameborder="0"
          height="400px"
          scrolling="no"
          width="60%"
        ></iframe>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="signInfoVisible"
      title="签约信息"
      width="62%"
    >
      <display-table :model="signInfo"></display-table>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){

    return{
      list: [{ name: "shang" }],
      user: this.$store.getters.user,
      signUrl: "",
      signInfo: {},
      accountBook: {},
      extCardInfo: {},
      signVisible: false,
      signInfoVisible: false,
    };
  },
  created(){
    this.getSignInfo();
  },
  methods: {
    async  onShowSignDialog(){
      let { branchid: id, branchname: name } = this.user;
      const response = await this.$alipay.get(`/api/AlipaySign/${id}`);
      if (response.data.isError) {
        this.signVisible = true;
        const response2 = await this.$alipay.get(`/api/AlipaySignUrl/${id}?merchantName=${name}`).then(res=>res.json());
        console.log(response2);
        this.signUrl = response2.signUrl;
      }else{
        this.signInfoVisible = true;
        this.signInfo = response.data;
        this.createAccountBook(id, response.data.agreementNo);
      }

    },
    async getSignInfo(){
      let { branchid: id } = this.user;
      const response = await this.$alipay.get(`/api/AlipaySign/${id}`);
      if (!response.data.isError) {
        this.signInfo = response.data;
        this.createAccountBook(id, response.data.agreementNo);
      }
    },
    async createAccountBook(merchantUserId, agreementNo){
      let body = {
        merchantUserId: merchantUserId.toString(),
        extInfo: {
          agreementNo
        }
      };
      console.log(body);
      const response = await this.$alipay.post("/api/AccountBook", body);

      console.log(response);
      this.accountBook = response.data;
      this.extCardInfo = this.accountBook.extCardInfo;
    },
    async  onRecharge(){
      let extInfo = `{"agreement_no":"${this.signInfo.agreementNo}"}`;

      let body = {
        transAmount: 0.1,
        remark: "remark",
        orderTitle: "title",
        payeeInfo: {
          identity: this.accountBook.accountBookId,
          extInfo: extInfo,
        },
      };

      const response = await this.$alipay.post("/api/PayOrder", body);
      console.log(response);
      this.rechageUrl = response.data.body;
    },
  }
};
</script>

<style>
</style>
